package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/models/placesservicenotification"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/notifications"
	"yundian/internel/pkg/utils"
)

func main() {
	//dingo.InitViper("./configs/adm.yaml")
	//dingo.InitViper("./configs/yundian.yaml")
	dingo.InitViper("./configs/zhongman.yaml")
	//dingo.InitViper("./configs/zmtest.yaml")
	//dingo.InitViper("./configs/zmtest-adm.yaml")
	dbs.NewMysqlComponent().Step()

	startTime := "2021-10-20 00:00:00"
	endTime := "2021-11-12 16:00:00"
	placeId := 31328
	PlaceName := "柏曼酒店云浮新兴县汽车站店"
	userIds := []int{10013, 10014, 10015}
	maxNum := 55
	maxLeve := 6
	rooms := CreateRoom(maxNum, maxLeve)

	//startTime := "2021-10-20 00:00:00"
	//endTime := "2021-11-12 16:00:00"
	//placeId := 444107
	//PlaceName := "柏曼酒店-东莞大岭山"
	//userIds := []int{10010,10011,10012}
	//maxNum := 80
	//maxLeve := 5
	//rooms := CreateRoom(maxNum, maxLeve)

	//startTime := "2021-10-20 00:00:00"
	//endTime := "2021-11-12 16:00:00"
	//placeId := 445028
	//PlaceName := "享莱行政公寓（深圳国际会展中心沙井地铁站店）"
	//userIds := []int{10007, 10008, 10009}
	//maxNum := 60
	//maxLeve := 5
	//rooms := CreateRoom(maxNum, maxLeve)

	days := utils.CreateDays(startTime, endTime, "2006-01-02 15:04:05", 60*10)
	var insertList []*placemodel.PlacesServiceNotification
	roomMap := map[string]int64{}

	for _, dataTime := range days {
		t, _ := time.Parse("2006-01-02 15:04:05", dataTime)

		PlaceRoom := SelectRoom(rooms)
		NotificationType := SelectType()
		if NotificationType == 9 {
		}

		if roomMap[PlaceRoom] != 0 && (t.Unix()-roomMap[PlaceRoom]) < 60*60*24 {
			continue
		}

		insert := &placemodel.PlacesServiceNotification{
			PlaceId:          placeId,
			UserId:           SelectUserId(userIds),
			MemberPhone:      "",
			PlaceName:        PlaceName,
			PlaceRoom:        PlaceRoom,
			DeviceSn:         "",
			NotificationType: NotificationType,
			SendType:         5,
			Status:           SelectStatus(),
			Info:             "",
			Message:          "",
			MakeTime:         dataTime,
			SendTime:         dataTime,
			CreateTime:       dataTime,
			UpdateTime:       dataTime,
		}
		roomMap[PlaceRoom] = t.Unix()
		if NotificationType == 5 {
			insert.ServiceInfo = []string{}
			insert.Info, insert.NoticeInfo = notifications.JointContentStr(insert)
		}
		if NotificationType == 6 {
			t1, _ := time.Parse("2006-01-02 15:04:05", dataTime)
			h5, _ := time.ParseDuration("-120h")
			endTime1 := t1.Add(h5).Format("2006-01-02")
			insert.ServiceInfo = []string{fmt.Sprintf("%s~%s", dataTime, endTime1), CreateOrder(), SelectApp()}
			insert.Info, insert.NoticeInfo = notifications.JointContentStr(insert)
		}
		insertList = append(insertList, insert)
	}
	global.Orm.Table("places_service_notification").Create(&insertList)
	fmt.Println(insertList)
}

func SelectApp() string {
	randNum := GetRand()
	if randNum > 0 && randNum <= 50 {
		return "美团续住"
	}
	return "携程续住"
}

func CreateOrder() string {
	return fmt.Sprintf("%d%d%d%d", GetRand, GetRand, GetRand, GetRand)
}

func SelectStatus() uint8 {
	randNum := GetRand()
	if randNum > 0 && randNum <= 5 {
		return uint8(4)
	}
	return uint8(6)
}

func SelectType() uint8 {
	randNum := GetRand()
	if randNum > 0 && randNum < 20 {
		return uint8(1)
	}
	if randNum >= 20 && randNum < 40 {
		return uint8(2)
	}
	if randNum >= 40 && randNum < 45 {
		return uint8(3)
	}
	if randNum >= 45 && randNum < 50 {
		return uint8(5)
	}
	if randNum >= 50 && randNum < 95 {
		return uint8(7)
	}
	if randNum >= 95 && randNum < 100 {
		return uint8(6)
	}
	fmt.Println("return uint8(9)")
	return uint8(9)
}

func SelectRoom(rooms []string) string {
	n := 100 / float32(len(rooms))
	for index, id := range rooms {
		targetRang := GetRand()
		if n*float32(index-1) < float32(targetRang) && float32(targetRang) <= n*float32(index) {
			return id
		}
	}

	for index, id := range rooms {
		targetRang := GetRand()
		if n*float32(index-1) < float32(targetRang) && float32(targetRang) <= n*float32(index) {
			return id
		}
	}
	return rooms[0]
}

func CreateRoom(maxNum, maxLeve int) []string {
	var rooms []string
	n := int(maxNum / maxLeve)
	for i := 2; i <= maxLeve; i++ {
		for j := 1; j <= n; j++ {
			if j < 10 {
				rooms = append(rooms, fmt.Sprintf("%d0%d", i, j))
			} else {
				rooms = append(rooms, fmt.Sprintf("%d%d", i, j))
			}
		}
	}
	return rooms
}

func SelectUserId(userIds []int) int {
	n := 100 / float32(len(userIds))
	for index, id := range userIds {
		targetRang := GetRand()
		if n*float32(index-1) < float32(targetRang) && float32(targetRang) <= n*float32(index) {
			return id
		}
	}

	for index, id := range userIds {
		targetRang := GetRand()
		if n*float32(index-1) < float32(targetRang) && float32(targetRang) <= n*float32(index) {
			return id
		}
	}
	return userIds[0]
}

func GetRand() int {
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	return int(r.Float32() * 100)
}

func HandleNotifications(param *placemodel.PlacesServiceNotification, userId int) {
	//通知房间号不能为空
	if param.PlaceRoom == "" || param.PlaceId <= 0 {
		log.Println("服务通知房间号为空", param)
		return
	}

	//拼接内容信息
	param.Info, param.NoticeInfo = notifications.JointContentStr(param)

	if param.MakeTime == "" {
		param.MakeTime = "1970-01-01 00:00:00"
	}

	//param.Status = 3
	//param.MemberId = userId
	//param.SendTime = time.Now().Format("2006-01-02 15:04:05")
	//param.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	//param.UpdateTime = time.Now().Format("2006-01-02 15:04:05")

	//新增通知
	notificationInfo := dao.NewNotificationDaoDao().NotificationServiceInsert(param).Unwrap().(*placemodel.PlacesServiceNotification)
	fmt.Println("成功", notificationInfo)
}
